<?php

function uploadImages( $arg = '', $uid = '' ){ 
	global $user;	
	$_uid = $uid ? $uid : $user->uid;
    $filename = time() . str_random(5) . '.png';
    $folder = 'sites/default/files/';   
    $result = file_put_contents($folder . $filename, file_get_contents('php://input')); 
    $image_server = api_file_save_data($folder . $filename, $filename, $_uid);   
    unlink($folder . $filename);   
	
    if ($image_server->fid) { 
        $json['fid'] = $image_server->fid;
        $json['url'] = $image_server->url_custom;
        $json['url_basic'] = $image_server->url_basic;      
        $json['image'] = $image_server->url ;        
        $json['thumb'] = $image_server->thubm;       
        $json['success'] = true;		
    }else{
        $json['fid'] = '';
        $json['avatar'] = '';
        $json['banner'] = '';
        $json['profile'] = '';
        $json['image'] = '';
        $json['success'] = false;
        $json['msg'] = 'quá trình upload bị lỗi';
    }         
    drupal_json_output($json);
}


function update_info_user(){
    global $user; 
    if(user_is_logged_in()){
       if(isset($_POST['update_u_txt_avatar']) && $_POST['update_u_txt_avatar'] > 0){
           //---------------Update avatar user
            db_update('users')
                ->fields(array(                        
                        'picture'  =>  $_POST['update_u_txt_avatar'] , 
                    ))
                ->condition('uid', $user->uid)
                ->execute();
            //---------------Update avatar user            
       } 
       /*-----------update_info_user----------------------------*/
        db_update('tbnd_info_user')
                ->fields(array(                        
                        'name'  =>  $_POST['txt_full_name'] , 
                        'address'  =>  $_POST['update_u_txt_address'] , 
                        'phone'  =>  $_POST['update_u_txt_phone'] , 
                        'tel'  =>  $_POST['update_u_txt_tel'] , 
                    ))
                ->condition('uid', $user->uid)
                ->execute();
       /*--------------------------------------------------------*/
       $json['status'] = true;
    }
    else{
        $json['status'] = false;
    }
     drupal_json_output($json);
}

function update_pass_user(){
	global $user; 
    if(user_is_logged_in()){
		require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
		if(!user_check_password($_POST['txt_old_pass'], $user)) {//kiem tra mat khau cu			 
			$a_error = array (
				'text_error' => 'Mật khẩu cũ chưa chính xác !' ,
				'class' => 'txt_old_pass' 
			);
			$list_error[] = $a_error;
			$json['status'] = FALSE;
			$json['list_error'] = $list_error;
		}
		elseif($_POST['txt_new_pass'] != $_POST['txt_new_pass_confirm']){
			$a_error = array (
				'text_error' => 'Mật khẩu không khớp !' ,
				'class' => 'txt_new_pass' 
			);
			$list_error[] = $a_error;
			$a_error = array (
				'text_error' => 'Mật khẩu không khớp !' ,
				'class' => 'txt_new_pass_confirm' 
			);
			$list_error[] = $a_error;
			$json['status'] = FALSE;
			$json['list_error'] = $list_error;
		}
		else{
			$item = array(
				'uid' => $user->uid,
				'pass' => user_hash_password($_POST['txt_new_pass'] ),
			);              
			drupal_write_record('users', $item, 'uid');
			$json['status'] = true;
		}
	}
	else{
		$json['status'] = FALSE;
	}
	drupal_json_output($json);
}

function news_pass_user(){ 
	global $user; 
    if(!user_is_logged_in()){
		require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
		if($_POST['txt_new_pass'] != $_POST['txt_new_pass_confirm']){
			$a_error = array (
				'text_error' => 'Mật khẩu không khớp !' ,
				'class' => 'txt_new_pass' 
			);
			$list_error[] = $a_error;
			$a_error = array (
				'text_error' => 'Mật khẩu không khớp !' ,
				'class' => 'txt_new_pass_confirm' 
			);
			$list_error[] = $a_error;
			$json['status'] = FALSE;
			$json['list_error'] = $list_error;
		}
		else{
			$item = array(
				'mail' => $_POST['txt_email_recover'],
				'pass' => user_hash_password($_POST['txt_new_pass'] ),
			);              
			drupal_write_record('users', $item, 'mail');
			$json['status'] = true;
		}
	}
	else{
		$json['status'] = FALSE;
	}
	drupal_json_output($json);
}


